package com.test.retry;

import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

@Service
public class RetryService {

    private int i = 0;

    /*
    启动类要加注解开始重试机制：@EnableRetry
     */
    @Retryable(value = {Exception.class}, maxAttempts = 5, backoff = @Backoff(value = 1000, multiplier = 1))
    public String retry() {
        try {
            System.err.println("调用重试======" + new Date());
            i++;
//            if (i == 3) {
//                int a = 1 / 1;
//            } else {
//            }
            int a = 1 / 0;
            return "abc";
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    @Recover
    public String testRecove() {
        System.out.println("执行5次以后调用了Recover" + new Date());
        // 直接在这里返回的话，接口调用方收到的就是"def"了
        return "def";
    }
}